﻿using DonetSpider.Db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DonetSpider
{
    public class SqliteWriterMovieDb : SqliteWriter, IMovieDb
    {
        readonly string title = "spider_title";
        readonly string item = "spider_items";

        public SqliteWriterMovieDb(string dbName) : base(dbName) {
        
        }
        public string Name { get; set; }
        public string PId { get; set; }

        public List<Title> SelectTitleById(string id)
        {
            var sql = $"select * from {title} where id = {id}";
            var data = new List<Title>();
            Select(sql, r =>
            {
                data.Add(new Title { Id = r.GetInt32(0), Name = r.GetString(2), StartUrl = r.GetString(3), SType = r.GetString(4) });

            });
            return data;
        }
        public List<Title> SelectTitle(string name)
        {
            var sql = string.IsNullOrEmpty(name) ? $"select * from {title}" : $"select * from {title} where d_Name like '%{name}%'";
            var data = new List<Title>();
            Select(sql, r =>
            {
                data.Add(new Title { Id = r.GetInt32(0), Name = r.GetString(2), StartUrl = r.GetString(3),SType = r.GetString(4),ImageUrl = r.GetString(5) });

            });
            return data;
        }
        public List<Title> SelectTitle()
        {
            var data = new List<Title>();
            Select($"select * from {title} where d_Name = '{Name}'", r =>
            {
                data.Add(new Title { Id = r.GetInt32(0), Name = r.GetString(2), StartUrl = r.GetString(3), SType = r.GetString(4),ImageUrl = r.GetString(5) });

            });
            return data;
        }
        public List<Items> SelectItems(string id)
        {
            var data = new List<Items>();
            Select($"select * from {item} where d_PId = '{id}'", r =>
            {
                data.Add(new Items { Id = r.GetInt32(0),PId = r.GetString(2), Name = r.GetString(3), DataMsg = r.GetString(4), Url = r.GetString(5), MUrl = r.GetString(6) });

            });
            return data;
        }
        public ItemsDto SelectItem(string id)
        {
            var data = new List<ItemsDto>();
            Select($@"select  i.*,t.d_name pname from {item} i inner join {title} t on i.d_pid = t.id  where i.id = '{id}' ", r =>
            {
                data.Add(new ItemsDto { Id = r.GetInt32(0),PId = r.GetString(2), Name = r.GetString(3), DataMsg = r.GetString(4), Url = r.GetString(5), MUrl = r.GetString(6),PName = r.GetString(7) });
            });
            return data.FirstOrDefault();
        }
        public Items SelectLastItem()
        {
            var data = new List<Items>();
            Select($"select * from {item} where d_PId = '{PId}'  ORDER BY ID DESC limit 1 ", r =>
            {
                data.Add(new Items { Id = r.GetInt32(0), Name = r.GetString(3), DataMsg = r.GetString(4), Url = r.GetString(5), MUrl = r.GetString(6) });
            });
            return data.FirstOrDefault();
        }
        public void WriteTitle(Title t)
        {
            Dictionary<string, string> data = new Dictionary<string, string>();
            data["Name"] = t.Name;
            data["StartUrl"] = t.StartUrl;
            data["SType"] = t.SType;
            data["ImageUrl"] = t.ImageUrl;
            Write(new List<Dictionary<string, string>> { data }, $"{title}");
        }
        public void WriteItems(List<Items> t)
        {
            var data = t.Select(m => new Dictionary<string, string>() {
                {"PId",m.PId},
                {"Name",m.Name},
                {"DataMsg",m.DataMsg},
                {"Url",m.Url},
                {"MUrl",m.MUrl},
            }).ToList();
            Write(data, $"{item}");
        }


    }
}
